package com.jilujia.domain.blog;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jilujia.domain.blog.event.BlogPostEvent;
import com.jilujia.domain.blog.repository.BlogRepository;
import com.jilujia.framework.domain.AbstractPoEntityManager;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class BlogManager extends AbstractPoEntityManager<Long, Blog, BlogRepository> {

	private static final Logger LOG = LoggerFactory.getLogger(BlogManager.class);

	@Transactional
	public void addReplyCount() {
		Blog blog = this.getInstance();
		blog.setReplyCount(blog.getReplyCount() + 1);
		this.save();
	}

	/**
	 * 
	 * @param firstPostFlag
	 *            是否是第一次发布Blog
	 */
	@Transactional
	public void saveBlog(boolean firstPostFlag) {
		this.save();
		if (firstPostFlag) {
			LOG.debug("fire BlogPostEvent");
			BlogPostEvent blogPostEvent = new BlogPostEvent(this, this.getInstance());
			this.getApplicationContext().publishEvent(blogPostEvent);
		}
	}
}
